package com.realone.packman.codec.decoder;

import com.realone.packman.codec.decoder.vo.BmsDecoderDataVo11;
import com.realone.packman.codec.exception.BmsDecoderException;

import java.nio.ByteBuffer;

/**
 * 获取BMS电压 解码
 *
 * @author hugx
 * @create 2018-09-12 16:16
 */
public class BmsDecoder11 extends BmsDecoder<BmsDecoderDataVo11> implements BmsDecoderStrategy {

    public BmsDecoder11(Integer classType) {
        this.classType = classType;
    }

    @Override
    public BmsDecoderDataVo11 decoderBody(ByteBuffer bb) {
        if (bb == null) {
            throw new BmsDecoderException(new NullPointerException());
        }

        BmsDecoderDataVo11 result = new BmsDecoderDataVo11(this.classType);
        /*  解析电池（包）的电压 */
        if (super.frameSerialNumber == 0xFFFF) {
            for (int i = 0; i < 30; i++) {
                result.addVoltage(decoderVoltage(bb));
            }
        } else {
            result.addVoltage(decoderVoltage(bb));
        }


        return result;
    }

    /**
     * 解析电池（包）的电压
     *
     * @param bb
     * @return
     */
    private Double decoderVoltage(ByteBuffer bb) {
        short voltage = bb.getShort();
        byte height = (byte) (voltage >>> 8);
        short low = (short) (voltage & 0x00FF);
        return (height * 256 + low) / 1000.0;

    }


}
